import types
from _collections import deque
from main.model.logic.pcb import PCB
from main.exceptions.exception import EmptyPCBGroupException


def prototypePCB():
    print "prototyping"
    """Prototype PCB to support group assignment"""
    def set_group(self, _group):
        self.group = _group
    def get_temporal_priority(self):
        return self.group.priority
    PCB.set_group = types.MethodType(set_group, PCB)
    PCB.get_temporal_priority = types.MethodType(get_temporal_priority, PCB)

class PCBGroup(object):
    
    prototypePCB()

    def __init__(self, priority_list):
        self.__priority_list = priority_list
        self.__pcbs = deque()
        
    def __nonzero__(self):
        return self.__pcbs and 1 or 0

    def add(self,pcb):
        pcb.set_group(self)
        self.__pcbs.append(pcb)

    def next(self):
        try:
            return self.__pcbs.popleft()
        except IndexError:
            raise EmptyPCBGroupException()

    @property
    def priority(self):
        return self.__priority_list.priority

    def size(self):
        return len(self.__pcbs)

    
    
